Skip to content

Add Solution.java for 0173 #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Add Solution.java for 0173 #190

wants to merge 2 commits into from

Conversation

toeii
Copy link
Member

@toeii toeii commented Jul 11, 2019

No description provided.

@toeii toeii changed the title leetcode 0173 java code Add Solution.java for 0173 Jul 11, 2019
Copy link
Member

@yanglbme yanglbme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @toeii ,请按照 doocs/leetcode 仓库的规范提交 leetcode 题解。

我推荐直接在 leetcode 平台上练习,你只需要将 AC 的代码放入 Solution.java 中,然后在 doocs/leetcode 上提交。

下面是我在你代码基础上做出的修改,以符合项目规范。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class BSTIterator {
    
    private TreeNode currentNode = null;
    private Stack<TreeNode> stack = new Stack<TreeNode>();

    public BSTIterator(TreeNode root) {
        if (root != null) {
            currentNode = root;
        }
    }
    
    /** @return the next smallest number */
    public int next() {
        while (currentNode != null) {
            stack.push(currentNode);
            currentNode = currentNode.left;
        }

        currentNode = stack.pop();
        TreeNode node = currentNode;
        currentNode = currentNode.right;

        return node.val;
    }
    
    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return currentNode != null || !stack.isEmpty();
    }
}

/**
 * Your BSTIterator object will be instantiated and called as such:
 * BSTIterator obj = new BSTIterator(root);
 * int param_1 = obj.next();
 * boolean param_2 = obj.hasNext();
 */

以前所有题解都采用该方式,为了确保项目的规范统一,此后所有提交的代码都需要按照以上方式。

@toeii toeii closed this Jul 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants